#!/usr/bin/python

import pygame
from pygame.locals import *
from gameobjects.vector2 import Vector2
import string

class MapLoader :

	def __init__( self, blockx, blocky, x_dim, y_dim, img_path, isActive ) :
		self.x = x_dim
		self.y = y_dim
		self.block_x = blockx
		self.block_y = blocky
		self.img_path = img_path
		self.image = None
		self.active = isActive
		self.upLink = None
		self.downLink = None
		self.rightLink = None
		self.leftLink = None
		self.restricted_pairs = []
		self.upEntranceExits = []
		self.downEntranceExits = []
		self.leftEntranceExits = []
		self.rightEntranceExits = []
		self.dEE_vec = Vector2(5*blockx,0*blocky)
		self.uEE_vec = Vector2(5*blockx,9*blocky)


	def loadLevel1( self ) :
		level1 = Map.Map(1,2)
		level1.add(0,0,Room.Room(0,0,640,640,64,64,'/home/thomp/pygame/thomp-rpg/build2/maps/level1/room1'))
		level1.add(0,1,Room.Room(0,1,640,640,64,64,'/home/thomp/pygame/thomp-rpg/build2/maps/level1/room2'))

	def getActiveMap( self ) :
		if self.upLink != None and self.upLink.isActive() :
			return self.upLink
		elif self.downLink != None and self.downLink.isActive() :
			return self.downLink
		elif self.leftLink != None and self.leftLink.isActive():
			return self.leftLink
		elif self.rightLink != None and self.rightLink.isActive():
			return self.rightLink
		else:
			return self

	def getUpEEVec( self ) :
		return self.uEE_vec

	def getDownEEVec( self ) :
		return self.dEE_vec

	def getUpEE( self ) :
		return self.upEntranceExits

	def getDownEE( self ) :
		return self.downEntranceExits

	def getImage( self ) :
		self.image = pygame.image.load(self.img_path).convert()
		return self.image

	def isActive( self ) :
		return self.active

	def toggleActive( self ):
		if self.active == True:
			self.active = False
		else:
			self.active = True

	def setUpLink( self, map_obj ) :
		self.upLink = map_obj

	def getUpLink( self ) :
		return self.upLink		
		
	def setDownLink( self, map_obj ) :
		self.downLink = map_obj		

	def getDownLink( self ) :
		return self.downLink

	def setRightLink( self, map_obj ) :
		self.rightLink = map_obj		

	def getRightLink( self ) :
		return self.rightLink

	def setLeftLink( self, map_obj ) :
		self.leftLink = map_obj

	def getLeftLink( self ) :
		return self.leftLink	

	def parseCSV( self, csvfile ) :
		xcount = 1
		ycount = 1
		csv = open(csvfile, 'r')
		for line in csv:
			print line
			for subline in line.split(','):
				if subline.find('x') != -1:
					self.restricted_pairs.append([xcount,ycount])
					xcount += 1
				if subline.find('eu') != -1:
					self.upEntranceExits.append([xcount,ycount])
					self.uEE_vec = Vector2((xcount-1)*self.block_x, (ycount-1)*self.block_y)						
					print 'uEE_vec:' + str(xcount-1) + '* block_x ' + str(ycount-1) + '* block_y'
					print 'upEntranceExits:' + str([xcount, ycount])
					xcount += 1
					continue
				if subline.find('ed') != -1:
					self.downEntranceExits.append([xcount,ycount])						
					self.dEE_vec = Vector2((xcount+1)*self.block_x, (ycount+1)*self.block_y)						
					print 'dEE_vec:' + str(xcount+1) + '* block_x ' + str(ycount+1) + '* block_y'
					print 'downEntranceExits:' + str([xcount, ycount])
					xcount += 1
					continue
				if subline.find('\n') != -1:
					ycount += 1
					xcount = 1
					continue
				if subline.find('x') == -1:
					xcount += 1
		print self.restricted_pairs

	def getRestrictedBlocks( self ) :
		return self.restricted_pairs

	def checkRestricted( self, current_pos ) :
		if [current_pos[0] + next_pos[0], current_pos[1] + next_pos[1]] in self.restricted_pairs:
			return False
		else:
			return True
		#return True		
						
	
